// cas_db1_h 型号电子称对接
const decoder = new TextDecoder('utf-8');
let tempValueArray = new Uint8Array([])
const beginMonitorReady = function () {
  return true
}
const endMonitorReady = function () {

}
const startReader = function () {
  tempValueArray = new Uint8Array([])
}
const valueDoWith = function (value, monitor) {

  let decodeBool = false
  let decodeValue = null

  let mergedArray = new Uint8Array(tempValueArray.length + value.length);
  mergedArray.set(tempValueArray);
  mergedArray.set(value, tempValueArray.length);
  tempValueArray = mergedArray

  let splitIndex = -1
  for (let i = tempValueArray.length - 1; i >= 0; i--) {
    if (tempValueArray[i] == 61) {
      splitIndex = i
      break;
    }
  }

  // 已读到完整数量
  if (splitIndex > 0) {
    let decodeStr = decoder.decode(tempValueArray.subarray(0, splitIndex).reverse());
    decodeValue = parseFloat(decodeStr)
    decodeBool = true
    let subArr2 = tempValueArray.subarray(splitIndex + 1)
    if (subArr2.length > 0) {
      tempValueArray = new Uint8Array(subArr2)
    }
    else {
      tempValueArray = new Uint8Array([])
    }
  }
  monitor(decodeValue,decodeBool)
}
export default {
  beginMonitorReady, startReader,
  endMonitorReady, valueDoWith,

}

